package com.renjun.smweb.service.RoleService.impl;

import com.alibaba.fastjson.JSONObject;
import com.renjun.smweb.commons.componet.BaseDaoComponent;
import com.renjun.smweb.commons.global.DataMap;
import com.renjun.smweb.commons.util.Format;
import com.renjun.smweb.commons.util.ResponseUtils;
import com.renjun.smweb.dao.bean.QueryParams;
import com.renjun.smweb.dao.bean.UpdateParams;
import com.renjun.smweb.dao.pojo.LoginInfo;
import com.renjun.smweb.service.RoleService.RoleService;
import com.renjun.smweb.commons.componet.BaseDaoComponent;
import com.renjun.smweb.dao.bean.InsertParams;
import com.renjun.smweb.dao.bean.Parameter;
import com.renjun.smweb.service.RoleService.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.SQLException;
import java.util.*;

/**
 * Created by huamin on 2017/2/21.
 */
@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private BaseDaoComponent baseDaoComponent;

    @Override
    public JSONObject queryRoleList(Map<String, Object> params) throws SQLException {
        LoginInfo loginInfo = (LoginInfo) params.remove("userinfo");
        QueryParams queryParams = QueryParams.createQueryParams("t_roleinfo");
        queryParams.addOrderColumns("roletype asc,specal asc,districtcode asc");
        Map<String, Object> map = new DataMap<>();
        for (Map.Entry<String, Object> entry : params.entrySet()) {
            if (entry.getValue() != null && !entry.getValue().toString().isEmpty())
                map.put(entry.getKey(), entry.getValue());
        }
        queryParams.addQueryParamsByMap(map);
        try {
            queryParams.setDbLog(loginInfo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        queryParams.printSelf();
        List<Map<String, Object>> result = baseDaoComponent.selectDataByParams(queryParams);
        JSONObject rs = ResponseUtils.createSuccessResponseBody("角色列表查看成功", result);
        return rs;
    }

    @Override
    public JSONObject addNewRole(Map<String, Object> params) throws SQLException {
        String districtcode="";
        String specal="0";
        if(params.get("districtcode")!=null){
            districtcode= (String) params.get("districtcode");
            specal="1";
        }
        LoginInfo loginInfo = (LoginInfo) params.remove("userinfo");
        InsertParams insertParams = InsertParams.createInsertParams("T_ROLEINFO",
                "ID,ROLENAME,ROLETYPE,DISTRICTCODE,ROLEDESC,ENABLED,ADDTIME,REMARK,SPECAL,ADDUSERID");
        insertParams.setValues(
                UUID.randomUUID().toString(),
                params.get("rolename"),
                params.get("roletype"),
                districtcode,
                params.get("roledesc"),
                "1",
                Format.getDateTime(),
                "",
                specal,
                loginInfo.getId()
        );
        try {
            insertParams.setDbLog(loginInfo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        baseDaoComponent.insertDataByParams(insertParams);
        JSONObject rs = ResponseUtils.createSuccessResponseBody("角色添加成功");
        return rs;
    }

    @Override
    public JSONObject updateRole(Map<String, Object> params) throws SQLException {
        LoginInfo loginInfo = (LoginInfo) params.remove("userinfo");
        UpdateParams updateParams = UpdateParams.createUpdateParams("t_roleinfo");
        updateParams.addWhereParameter(Parameter.createParameter("id", params.get("roleid")));
        updateParams.addParam(Parameter.createParameter("rolename", params.get("rolename")));
        updateParams.addParam(Parameter.createParameter("roletype", params.get("roletype")));
        updateParams.addParam(Parameter.createParameter("roledesc", params.get("roledesc")));
        updateParams.addParam(Parameter.createParameter("updatetime", Format.getDateTime()));
        updateParams.addParam(Parameter.createParameter("updateuserid", loginInfo.getId()));
        try {
            updateParams.setDbLog(loginInfo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        baseDaoComponent.updateDataByParams(updateParams);
        JSONObject rs = ResponseUtils.createSuccessResponseBody("角色修改成功");
        return rs;
    }

    @Override
    public JSONObject recoverOrCancelRole(Map<String, Object> params) throws SQLException {
        //TODO 1，是否要删除t_admininfo？2，加事务
        LoginInfo loginInfo = (LoginInfo) params.remove("userinfo");
        UpdateParams updateParams = UpdateParams.createUpdateParams("t_roleinfo");
        updateParams.addWhereParameter(Parameter.createParameter("id", params.get("roleid")));
        updateParams.addParam(Parameter.createParameter("enabled", params.get("enabled")));
        try {
            updateParams.setDbLog(loginInfo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        baseDaoComponent.updateDataByParams(updateParams);
        JSONObject rs = ResponseUtils.createSuccessResponseBody("角色修改成功");
        return rs;
    }

    @Override
    public JSONObject queryRoleById(Map<String, Object> params) throws SQLException {
        LoginInfo loginInfo = (LoginInfo) params.remove("userinfo");
        QueryParams queryParams = QueryParams.createQueryParams("t_roleinfo");
        queryParams.addQueryParams(Parameter.createParameter("id", params.get("roleid")));
        try {
            queryParams.setDbLog(loginInfo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        List<Map<String, Object>> result = baseDaoComponent.selectDataByParams(queryParams);
        JSONObject rs = ResponseUtils.createSuccessResponseBody("角色查看成功", result);
        return rs;
    }
}
